package br.edu.utfpr.bsi.bo.xmlrpc;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Vector;

import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;

import br.edu.utfpr.bsi.model.Livro;

public class LivroXMLRpcBO {
	
	XmlRpcClientConfigImpl config;
	XmlRpcClient client;
	
	public LivroXMLRpcBO(String server) {
		config = new XmlRpcClientConfigImpl();
		client = new XmlRpcClient();
		
		try {
			config.setServerURL(new URL(server));
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		client.setConfig(config);
	}
	
	public String cadastrarLivro(Object obj) {
		
		Livro l = (Livro) obj;
		Vector<Object> params = new Vector<Object>(); 
		
		params.addElement(l.getCodigo());
		params.addElement(l.getTitulo());
		params.addElement(l.getEdicao());
		params.addElement(l.getAno());
		params.addElement(l.getAutor());
		params.addElement(l.getEditora());

		Object result;
		try {
			result = client.execute("Cadastrar", params);
		} catch (XmlRpcException e) {
			e.printStackTrace();
			return null;
		} 
		
		return result.toString();
	}
	
	public String registrarVendaLivroByCod(String cod) {
		
		Vector<Object> params = new Vector<Object>(); 
		
		params.addElement(cod);

		Object result;
		try {
			result = client.execute("Vender", params);
		} catch (XmlRpcException e) {
			e.printStackTrace();
			return null;
		} 
		
		return result.toString();
	}
}
